﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Computer_Manager.DTO;

namespace Computer_Manager.DAL
{
    class DAL_product
    {
        DbProvider db = new DbProvider();
        SqlCommand cmd;

        public void product_insert(DTO_product dt)
        {
            db.Open();
            cmd = new SqlCommand("product_Insert", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@codes_pro", SqlDbType.VarChar).Value = dt.codes_pro;
            cmd.Parameters.Add("@name_pro", SqlDbType.NVarChar).Value = dt.name_pro;
            cmd.Parameters.Add("@price_in", SqlDbType.Money).Value = dt.price_in;
            cmd.Parameters.Add("@amount_in", SqlDbType.Int).Value = dt.amount_in;
            cmd.Parameters.Add("@price_out", SqlDbType.Money).Value = dt.price_out;
            cmd.Parameters.Add("@date_in", SqlDbType.Date).Value = dt.date_in;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void product_Update(DTO_product dt)
        {
            db.Open();
            cmd = new SqlCommand("product_Update", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_pro", SqlDbType.Int).Value = dt.id_pro;
            cmd.Parameters.Add("@codes_pro", SqlDbType.VarChar).Value = dt.codes_pro;
            cmd.Parameters.Add("@name_pro", SqlDbType.NVarChar).Value = dt.name_pro;
            cmd.Parameters.Add("@price_in", SqlDbType.NVarChar).Value = dt.price_in;
            cmd.Parameters.Add("@amount_in", SqlDbType.Int).Value = dt.amount_in;
            cmd.Parameters.Add("@price_out", SqlDbType.NVarChar).Value = dt.price_out;
            cmd.Parameters.Add("@date_in", SqlDbType.Date).Value = dt.date_in;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void product_Delete(DTO_product dt)
        {
            db.Open();
            cmd = new SqlCommand("product_Delete", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_pro", SqlDbType.Int).Value = dt.id_pro;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            db.getConn().Close();
        }
        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("product_SelectAll", db.getConn());
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }
        public DataTable GetByID(DTO_product dt)
        {
            cmd = new SqlCommand("product_GetByID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_pro", SqlDbType.Int).Value = dt.id_pro;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByCat(DTO_product dt)
        {
            cmd = new SqlCommand("product_GetByCat", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_cat", SqlDbType.Int).Value = dt.id_cat;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        


    }
}
